package com.kws.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.SysKnowledgeBase;
import com.kws.system.domain.info.QueryKnowledgeBaseInfo;
import com.kws.system.domain.vo.CreateKnowledgeBaseRq;
import com.kws.system.domain.vo.UpdateKnowledgeBaseRq;
import com.kws.system.mapper.SysKnowledgeBaseMapper;
import com.kws.system.service.SysKnowledgeBaseService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>SysKnowledgeBaseServiceImpl</b>
 *
 * @description: SysKnowledgeBaseServiceImpl <br>
 * @date: 2024/11/4 14:57 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class SysKnowledgeBaseServiceImpl extends ServiceImpl<SysKnowledgeBaseMapper, SysKnowledgeBase> implements SysKnowledgeBaseService {
    @Override
    public Page<QueryKnowledgeBaseInfo> queryKnowledgeBasePage(Integer pageNum, Integer pageSize, String content) {
        Long companyId = SecurityUtils.getCompanyId();
        Page<QueryKnowledgeBaseInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryKnowledgeBaseInfo> pageInfo = baseMapper.queryKnowledgeBasePage(page,content,companyId);
        return pageInfo;
    }

    @Override
    public boolean createKnowledgeBase(CreateKnowledgeBaseRq rq) {
        SysKnowledgeBase sysKnowledgeBase = new SysKnowledgeBase();
        sysKnowledgeBase.setCompanyId(SecurityUtils.getCompanyId());
        sysKnowledgeBase.setUserType(rq.getUserType());
        sysKnowledgeBase.setProblemType(rq.getProblemType());
        sysKnowledgeBase.setConcreteProblem(rq.getConcreteProblem());
        sysKnowledgeBase.setSolution(rq.getSolution());
        Ksys.setBasicModelData(sysKnowledgeBase,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(sysKnowledgeBase);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public QueryKnowledgeBaseInfo queryKnowledgeBaseById(Long id) {
        return baseMapper.queryKnowledgeBaseById(id);
    }

    @Override
    public boolean updateKnowledgeBaseById(UpdateKnowledgeBaseRq rq) {
        SysKnowledgeBase sysKnowledgeBase = baseMapper.selectById(rq.getId());
        if (null == sysKnowledgeBase){
            throw new SecurityException("参数错误！");
        }
        sysKnowledgeBase.setUserType(rq.getUserType());
        sysKnowledgeBase.setProblemType(rq.getProblemType());
        sysKnowledgeBase.setConcreteProblem(rq.getConcreteProblem());
        sysKnowledgeBase.setSolution(rq.getSolution());
        Ksys.setBasicModelData(sysKnowledgeBase,SecurityUtils.getStringUserId(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
        int i = baseMapper.updateById(sysKnowledgeBase);
        if (1 == i){
            return true;
        }
        return false;
    }

    @Override

    public boolean deleteKnowledgeBaseById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
